﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QuanLyKTX
{
    using Telerik.WinControls.UI;
    using DAL;
    using Telerik.WinControls.UI.Docking;
    using Telerik.WinControls.Enumerations;

    public partial class frm_Rooms : RadForm
    {
        private RadContextMenu contexMenu;

        #region Private methods
        
        private void BindGridView()
        {
            using (var db = new QuanLyKTXDataContext())
            {
                var results = from p in db.tbl_Phongs
                              select new
                              {
                                  p.SoPhong,
                                  p.SoGiuongTrong,
                                  p.TongSoGiuong,
                                  LoaiPhong = p.LoaiPhong == true ? "Nam" : "Nữ"
                              };

                radGridView_Phong.DataSource = results;
            }
        }

        private void InsPhong(string soPhong, bool loaiPhong, byte tongGiuong)
        {
            using (var db = new QuanLyKTXDataContext())
            {
                var results = from p in db.tbl_Phongs
                              where p.SoPhong == soPhong
                              select p;

                if (results.Count() > 0)
                {
                    MessageBox.Show("Số phòng đã có sẵn.");
                }
                else
                {
                    db.tbl_Phongs.InsertOnSubmit(new tbl_Phong() { SoPhong = soPhong, LoaiPhong = loaiPhong, TongSoGiuong = tongGiuong, SoGiuongTrong = tongGiuong });
                    db.SubmitChanges();
                    BindGridView();
                    ResetForm();
                }
            }
        }

        private void InitGridViewContextMenu()
        {
            contexMenu = new RadContextMenu();
            RadMenuItem menuItemXoa = new RadMenuItem("Xóa");
            menuItemXoa.Click += new EventHandler(menuItemXoa_Click);
            RadMenuItem menuItemSua = new RadMenuItem("Sửa");
            menuItemSua.Click += new EventHandler(menuItemSua_Click);

            contexMenu.Items.Add(menuItemXoa);
            contexMenu.Items.Add(menuItemSua);
        }

        private void ResetForm()
        {
            radTextBox_SoPhong.Clear();
            radMaskedEditBox_TongGiuong.Value = null;
            radTextBox_SoPhong.Focus();
            radButton_BoQua.Visible = false;
            radTextBox_SoPhong.ReadOnly = false;
        }

        private void LoadDataEdit(string soPhong)
        {
            using (var db = new QuanLyKTXDataContext())
            {
                var result = (from p in db.tbl_Phongs
                              where p.SoPhong == soPhong
                              select p).FirstOrDefault();
                if (result != null)
                {
                    radTextBox_SoPhong.Text = result.SoPhong;
                    if (Convert.ToBoolean(result.LoaiPhong))
                        radRadioButton_Nam.ToggleState = ToggleState.On;
                    else
                        radRadioButton_Nu.ToggleState = ToggleState.On;
                    radMaskedEditBox_TongGiuong.Text = result.TongSoGiuong.ToString();
                    radMaskedEditBox_TongGiuong.Value = result.TongSoGiuong;
                    radButton_BoQua.Visible = true;
                    radTextBox_SoPhong.ReadOnly = true;

                }
            }
        }

        private void UpdPhong(string soPhong, bool loaiPhong, byte tongGiuong)
        {
            using (var db = new QuanLyKTXDataContext())
            {
                //Kiểm tra xem phòng có người?
                int checkResult = (from p in db.tbl_XepPhongs
                                   where p.SoPhong == soPhong && p.NgayRa == null
                                   select p).Count();


                var result = db.tbl_Phongs.Where(p => p.SoPhong == soPhong).Single();
                result.LoaiPhong = loaiPhong;
                result.TongSoGiuong = tongGiuong;
                result.SoGiuongTrong = checkResult > 0 ? Convert.ToByte(checkResult) : Convert.ToByte(tongGiuong);

                db.SubmitChanges();
                BindGridView();
                ResetForm();
            }
        }

        private void DelPhong(string soPhong)
        {
            DialogResult dlgResult = MessageBox.Show("Bạn có chắc không sử dụng thông tin này?", "Cảnh báo", MessageBoxButtons.YesNo);

            if (dlgResult.Equals(DialogResult.Yes))
            {
                using (var db = new QuanLyKTXDataContext())
                {
                    var results = from p in db.tbl_Phongs
                                  where p.SoPhong == soPhong && p.TongSoGiuong == p.SoGiuongTrong
                                  select p;
                    if (results.Count() > 0)
                    {
                        db.tbl_Phongs.DeleteOnSubmit(db.tbl_Phongs.Where(p => p.SoPhong == soPhong).FirstOrDefault());
                        db.SubmitChanges();
                        BindGridView();
                    }
                    else
                    {
                        MessageBox.Show("Phòng đang được thuê. Không thể xóa.");
                    }
                }
            }
        }

        #endregion

        #region Event methods

        public frm_Rooms()
        {
            InitializeComponent();
        }

        private void frm_Rooms_Load(object sender, EventArgs e)
        {
            BindGridView();
            InitGridViewContextMenu();
        }

        private void radButton_Luu_Click(object sender, EventArgs e)
        {
            if (!radButton_BoQua.Visible)
            {
                if (radTextBox_SoPhong.Text.Trim().Length > 0)
                {
                    InsPhong(radTextBox_SoPhong.Text.Trim().ToUpper(), radRadioButton_Nam.IsChecked, radMaskedEditBox_TongGiuong.Value.ToString() != string.Empty ? Convert.ToByte(radMaskedEditBox_TongGiuong.Value) : Convert.ToByte(0));
                }
                else
                    MessageBox.Show("Cần phải nhập Số phòng.");
            }
            else
            {
                UpdPhong(radTextBox_SoPhong.Text, radRadioButton_Nam.IsChecked, radMaskedEditBox_TongGiuong.Value != null ? Convert.ToByte(radMaskedEditBox_TongGiuong.Text) : Convert.ToByte(0));
            }
        }

        void menuItemSua_Click(object sender, EventArgs e)
        {
            LoadDataEdit(radGridView_Phong.CurrentRow.Cells[1].Value.ToString());
        }

        void menuItemXoa_Click(object sender, EventArgs e)
        {
            DelPhong(radGridView_Phong.CurrentRow.Cells[1].Value.ToString());
        }

        private void radGridView_Phong_ContextMenuOpening(object sender, ContextMenuOpeningEventArgs e)
        {
            if (!radGridView_Phong.CurrentRow.IsSystem)
            {
                e.ContextMenu = contexMenu.DropDown;
            }
        }

        private void radButton_BoQua_Click(object sender, EventArgs e)
        {
            ResetForm();
        }

        #endregion

    }
}
